﻿using System.Collections.Generic;
using System.Web.Mvc;
namespace QuocHoi_TV_Frontend.Models
{
    public static class HtmlHelperSiteMenu
    {
        public static MvcHtmlString SiteMenuHorizal(this HtmlHelper helper, List<ISiteLink> siteLinks,long CatalogID)
        {
            if (siteLinks == null || siteLinks.Count == 0)
                return MvcHtmlString.Empty;
            var topLevelParentId = SiteLinkListHelper.GetTopLevelParentId(siteLinks);
            return MvcHtmlString.Create(buildMenuItems(siteLinks, topLevelParentId, CatalogID));
        }
        public static MvcHtmlString SiteMenuVertical(this HtmlHelper helper, List<ISiteLink> siteLinks, long CatalogID)
        {
            if (siteLinks == null || siteLinks.Count == 0)
                return MvcHtmlString.Empty;
            var topLevelParentId = SiteLinkListHelper.GetTopLevelParentId(siteLinks);
            return MvcHtmlString.Create(SiteMenuVertical(siteLinks, topLevelParentId, CatalogID));
        }
        public static MvcHtmlString SiteMenuFooter(this HtmlHelper helper, List<ISiteLink> siteLinks, long CatalogID)
        {
            if (siteLinks == null || siteLinks.Count == 0)
                return MvcHtmlString.Empty;
            var topLevelParentId = SiteLinkListHelper.GetTopLevelParentId(siteLinks);
            return MvcHtmlString.Create(SiteMenuFooter(siteLinks, topLevelParentId, CatalogID));
        }
        private static string buildMenuItems(List<ISiteLink> siteLinks, int parentId,long CatalogCurrID)
        {
            var parentTag = new TagBuilder("ul");
            parentTag.AddCssClass("top_menu");
            List<ISiteLink> childSiteLinks = SiteLinkListHelper.GetChildSiteLinks(siteLinks, parentId);
            for (int i = 0; i < childSiteLinks.Count; i++)
            {
                var itemTag = new TagBuilder("li");
                 if (i == childSiteLinks.Count - 1)
                {
                    itemTag.AddCssClass("lastmenutop");
                }
                else {
                    itemTag.AddCssClass("");
                }
                 if (CatalogCurrID == childSiteLinks[i].Id)
                {
                    itemTag.AddCssClass("active");
                }
                else
                {
                    if (childSiteLinks[i].Id == 1 && CatalogCurrID == -1)
                    {
                        itemTag.AddCssClass("active");
                    }
                }
                var itemli= new TagBuilder("li");
                itemli.AddCssClass("border");
                var anchorTag = new TagBuilder("a");
                anchorTag.MergeAttribute("href", childSiteLinks[i].Url);

                if (CatalogCurrID == childSiteLinks[i].Id)
                {
                    anchorTag.AddCssClass("active");
                }
               else if (siteLinks[i].Id == 1)
                {
                    if (childSiteLinks[i].Id == 1 && CatalogCurrID == -1)
                    {
                        anchorTag.AddCssClass("active");
                    }
                    
                }
               
                if (childSiteLinks[i].OpenInNewWindow)
                {
                    anchorTag.MergeAttribute("target", "_blank");
                
                }
                anchorTag.SetInnerText(childSiteLinks[i].Text);
                itemTag.InnerHtml = anchorTag.ToString() + itemli.ToString();
                
                if (SiteLinkListHelper.SiteLinkHasChildren(siteLinks, childSiteLinks[i].Id))
                {
                   // itemTag.AddCssClass("expanded");
                   // itemTag.InnerHtml += buildMenuItems(siteLinks, childSiteLinks[i].Id, CatalogCurrID);
                }
                parentTag.InnerHtml += itemTag;
            }
            return parentTag.ToString();
        }
        private static string SiteMenuVertical(List<ISiteLink> siteLinks, int parentId, long CatalogCurrID)
        {
            var parentTag = new TagBuilder("ul");
            parentTag.AddCssClass("");
            var itemdiv = new TagBuilder("div");
            itemdiv.AddCssClass("line1");
            List<ISiteLink> childSiteLinks = SiteLinkListHelper.GetChildSiteLinks(siteLinks, parentId);
            for (int i = 0; i < childSiteLinks.Count; i++)
            {
                var itemTag = new TagBuilder("li");
               
                if (CatalogCurrID == parentId)
                {
                    itemTag.AddCssClass("activeH");
                }
                var anchorTag = new TagBuilder("a");
                anchorTag.MergeAttribute("href", childSiteLinks[i].Url);
                if (childSiteLinks[i].OpenInNewWindow)
                {
                    anchorTag.MergeAttribute("target", "_blank");
                
                }
                anchorTag.SetInnerText(childSiteLinks[i].Text);
                itemTag.InnerHtml =anchorTag.ToString() ;
                
                if (SiteLinkListHelper.SiteLinkHasChildren(siteLinks, childSiteLinks[i].Id))
                {
                   // itemTag.AddCssClass("expanded");
                   // itemTag.InnerHtml += buildMenuItems(siteLinks, childSiteLinks[i].Id, CatalogCurrID);
                }
                parentTag.InnerHtml += itemdiv.ToString() + itemTag;
            }
            return parentTag.ToString();
        }
        private static string SiteMenuFooter(List<ISiteLink> siteLinks, int parentId, long CatalogCurrID)
        {
            var parentTag = new TagBuilder("div");
            parentTag.AddCssClass("menufooter");
            List<ISiteLink> childSiteLinks = SiteLinkListHelper.GetChildSiteLinks(siteLinks, parentId);
            for (int i = 0; i < childSiteLinks.Count; i++)
            {
                var anchorTag = new TagBuilder("a");
                if (CatalogCurrID == parentId)
                {
                    anchorTag.AddCssClass("active");
                }
                else if (siteLinks[i].Id == 1)
                {
                    anchorTag.AddCssClass("active");
                }
                if (i == childSiteLinks.Count-1)
                {
                    anchorTag.AddCssClass("last");
                }
                anchorTag.MergeAttribute("href", childSiteLinks[i].Url);
                if (childSiteLinks[i].OpenInNewWindow)
                {
                    anchorTag.MergeAttribute("target", "_blank");
                }
                anchorTag.SetInnerText(childSiteLinks[i].Text);
                parentTag.InnerHtml += anchorTag.ToString() ;
            }
            return parentTag.ToString();
        }
    }
}